/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Clases.InfoBox;
import Clases.cPaciente;
import Clases.eTipoDocumento;
import DB_Manager.SessionFactoryUtil;
import java.util.Calendar;
import javax.xml.bind.ParseConversionEvent;

/**
 *
 * @author santiago
 */
public class dlg_Paciente extends javax.swing.JDialog {

    /**
     * Creates new form dlg_Paciente
     */
    private cPaciente Paciente;

    public dlg_Paciente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        Calendar now = Calendar.getInstance();

        jCB_FN_ano.removeAllItems();
        for (int i = (now.get(Calendar.YEAR) - 100); i < now.get(Calendar.YEAR) + 1; i++) {
            jCB_FN_ano.addItem(i);
        }

        jCB_FN_ano.setSelectedItem(now.get(Calendar.YEAR));

        jCB_FN_mes.removeAllItems();
        for (int i = 1; i <= 12; i++) {
            jCB_FN_mes.addItem(i);
        }

        jCB_FN_mes.setSelectedItem(1);

        jCB_FN_dia.removeAllItems();
        for (int i = 1; i <= now.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
            jCB_FN_dia.addItem(i);
        }

        jCB_FN_mes.setSelectedItem(1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTF_apellido = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTF_nombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jCB_FN_dia = new javax.swing.JComboBox();
        jCB_FN_mes = new javax.swing.JComboBox();
        jCB_FN_ano = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jCB_TipoDocumento = new javax.swing.JComboBox();
        jTF_numerodocumento = new javax.swing.JTextField();
        jTF_telefono = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTF_direccion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTF_detalle = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Apellido:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Fecha Nacimiento:");

        jCB_FN_mes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCB_FN_mesItemStateChanged(evt);
            }
        });
        jCB_FN_mes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCB_FN_mesActionPerformed(evt);
            }
        });

        jCB_FN_ano.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCB_FN_anoItemStateChanged(evt);
            }
        });
        jCB_FN_ano.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCB_FN_anoActionPerformed(evt);
            }
        });

        jLabel4.setText("Documento:");

        jLabel5.setText("Telefono:");

        jCB_TipoDocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DNI", "CEDULA", "LC", "LE", "PASAPORTE" }));

        jLabel6.setText("Direccion:");

        jLabel7.setText("Detalles:");

        jTF_detalle.setColumns(20);
        jTF_detalle.setRows(5);
        jScrollPane1.setViewportView(jTF_detalle);

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Guargar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTF_apellido))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTF_nombre))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jCB_FN_ano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jCB_FN_mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jCB_FN_dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jCB_TipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTF_numerodocumento))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTF_telefono))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jTF_direccion))
                        .addComponent(jLabel7)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTF_apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTF_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jCB_FN_dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCB_FN_mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCB_FN_ano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jCB_TipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTF_numerodocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTF_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTF_direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCB_FN_anoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCB_FN_anoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCB_FN_anoActionPerformed

    private void jCB_FN_mesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCB_FN_mesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCB_FN_mesActionPerformed

    private void jCB_FN_anoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCB_FN_anoItemStateChanged
        // TODO add your handling code here:

        int ano = 1900;
        if (jCB_FN_ano.getSelectedItem() != null) {
            ano = Integer.parseInt(jCB_FN_ano.getSelectedItem().toString());
        }

        int mes = 1;
        if (jCB_FN_mes.getSelectedItem() != null) {
            mes = Integer.parseInt(jCB_FN_mes.getSelectedItem().toString());
        }

        this.SetMaxDay(ano, mes);
    }//GEN-LAST:event_jCB_FN_anoItemStateChanged

    private void jCB_FN_mesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCB_FN_mesItemStateChanged
        // TODO add your handling code here:

        int ano = 1900;
        if (jCB_FN_ano.getSelectedItem() != null) {
            ano = Integer.parseInt(jCB_FN_ano.getSelectedItem().toString());
        }

        int mes = 1;
        if (jCB_FN_mes.getSelectedItem() != null) {
            mes = Integer.parseInt(jCB_FN_mes.getSelectedItem().toString());
        }

        this.SetMaxDay(ano, mes);
    }//GEN-LAST:event_jCB_FN_mesItemStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:

        if (this.Paciente == null) {
            this.Paciente = new cPaciente();
        }

        this.Paciente.setApellido(jTF_apellido.getText());
        this.Paciente.setNombre(jTF_nombre.getText());

        Calendar cal = Calendar.getInstance();

        cal.set(Integer.parseInt(jCB_FN_ano.getSelectedItem().toString()), Integer.parseInt(jCB_FN_mes.getSelectedItem().toString()), Integer.parseInt(jCB_FN_dia.getSelectedItem().toString()));

        this.Paciente.setFechaNacimiento(cal);
        this.Paciente.setTipoDocumento(eTipoDocumento.getTipoDocumentoFromIndex(jCB_TipoDocumento.getSelectedIndex()));
        this.Paciente.setDocumento(jTF_numerodocumento.getText());
        this.Paciente.setTelefono(jTF_telefono.getText());
        this.Paciente.setDireccion(jTF_direccion.getText());
        this.Paciente.setDetalle(jTF_detalle.getText());

        SessionFactoryUtil.Save(this.Paciente);

        InfoBox.infoBox("Los datos del paciente se guardaron correctamente", "Datos Guardados");

        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        InfoBox.infoBox("¿Está seguro que quiere salir sin guardar el paciente?", "Cancelar");
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void SetMaxDay(int ano, int mes) {
        Calendar now = Calendar.getInstance();
        now.set(ano, mes - 1, 1);

        int index = jCB_FN_dia.getSelectedIndex();

        jCB_FN_dia.removeAllItems();
        for (int i = 1; i <= now.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
            jCB_FN_dia.addItem(i);
        }

        if (index > jCB_FN_dia.getItemCount()) {
            index = 0;
        }

        jCB_FN_dia.setSelectedIndex(index);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlg_Paciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlg_Paciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlg_Paciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlg_Paciente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlg_Paciente dialog = new dlg_Paciente(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jCB_FN_ano;
    private javax.swing.JComboBox jCB_FN_dia;
    private javax.swing.JComboBox jCB_FN_mes;
    private javax.swing.JComboBox jCB_TipoDocumento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTF_apellido;
    private javax.swing.JTextArea jTF_detalle;
    private javax.swing.JTextField jTF_direccion;
    private javax.swing.JTextField jTF_nombre;
    private javax.swing.JTextField jTF_numerodocumento;
    private javax.swing.JTextField jTF_telefono;
    // End of variables declaration//GEN-END:variables

    public void setPaciente(cPaciente Paciente) {
        this.Paciente = Paciente;

        this.jTF_apellido.setText(this.Paciente.getApellido());
        this.jTF_nombre.setText(this.Paciente.getNombre());
        this.jCB_TipoDocumento.setSelectedIndex(this.Paciente.getTipoDocumento().getValue());
        this.jCB_FN_ano.setSelectedItem(this.Paciente.getFechaNacimiento().get(Calendar.YEAR));
        this.jCB_FN_mes.setSelectedItem(this.Paciente.getFechaNacimiento().get(Calendar.MONTH) + 1);
        this.jCB_FN_dia.setSelectedItem(this.Paciente.getFechaNacimiento().get(Calendar.DAY_OF_MONTH));
        this.jTF_numerodocumento.setText(this.Paciente.getDocumento());
        this.jTF_telefono.setText(this.Paciente.getTelefono());
        this.jTF_direccion.setText(this.Paciente.getDireccion());
        this.jTF_detalle.setText(this.Paciente.getDetalle());
    }
}
